import 'package:flutter/material.dart';
import 'package:sample2/helper/app_helper.dart';
import 'package:sample2/helper/chat_helper.dart';
import 'package:sample2/model/chat/chat_user.dart';
import 'package:sample2/model/user_info.dart';
import 'package:sample2/pages/chat/chat_conversation.dart';

class AnonymousChatItem extends StatefulWidget {
  final ChatMessageUser user;
  const AnonymousChatItem({super.key, required this.user});

  @override
  State<StatefulWidget> createState() {
    return AnonymousChatItemState();
  }
}

class AnonymousChatItemState extends State<AnonymousChatItem> {
  String? _username;

  @override
  void initState() {
    AppHelper.shared.getUserInfo().then((UserInfo? userInfo) {
      if(mounted){
        setState(() {
          _username = userInfo?.username;
        });
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      highlightColor: const Color.fromARGB(137, 116, 106, 207),
      onTap: () {
        if (widget.user.userName == AppHelper.shared.userInfo?.username) {
          return;
        }
        ChatHelper.updateCurrentToUser(widget.user);
        Navigator.of(context).pushNamed("/chatMessage", arguments: {
          "toUser": widget.user,
          "kind": ChatConversationKind.peer2peer
        });
      },
      child: Ink(
        child: Container(
          padding: const EdgeInsets.all(13),
          constraints: const BoxConstraints(minHeight: 50, maxHeight: 80),
          child: Row(children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(widget.user.userName),
                const Spacer(),
                Text("Status: ${widget.user.getStatusDesc()}")
              ],
            ),
            const Spacer(),
            Column(
              children: [
                Text(widget.user.userName == _username ? "Me" : ""),
                const Spacer(),
                Text(
                  widget.user.lastMessage ?? "",
                  textAlign: TextAlign.end,
                ),
              ],
            )
          ]),
        ),
      ),
    );
  }
}
